<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>登录—注册页面 | </title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <!--<link href="css/nprogress.css" rel="stylesheet">-->
    <!-- Animate.css -->
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/button.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="css/custom.min.css" rel="stylesheet">
</head>

<body class="login">
<div>
    <a class="hiddenanchor" id="signup"></a>
    <a class="hiddenanchor" id="signin"></a>

    <div class="login_wrapper">
        <div class="animate form login_form">
            <section class="login_content">
                <form>
                    <h1>GLS国际物流系统 </h1>
                    <div>
                        <input type="text" class="form-control" placeholder="用户名" id="userName" required="" />
                    </div>
                    <div>
                        <input type="password" class="form-control" id="pwd" placeholder="密&nbsp;&nbsp;&nbsp;码" required="" /><span style="color:red;" id="span2"></span>
                    </div>
                    <div>
                        <a class="btn btn-default submit" id="login" href="javascript:;">登 录</a>
                        <a class="btn btn-default submit" href="misspwd.html">忘记密码</a>
                    </div>

                    <div class="clearfix"></div>

                    <div class="separator">
                        <p class="change_link">网站新手?
                            <a href="#signup" class="to_register"> 创建账号</a>
                        </p>

                        <div class="clearfix"></div>
                        <br />

                        <div>
                            <h1><i class="fa fa-paw"></i> 美的&nbsp;&nbsp;&nbsp;集团</h1>
                            <p>©2019 All Rights Reserved. </p>
                        </div>
                    </div>
                </form>
            </section>
        </div>

        <div id="register" class="animate form registration_form">
            <section class="login_content">
                <form action="">
                    <h1>注册账号</h1>

                    <div>
                        <input style="margin:0" type="text" class="form-control" placeholder="用&nbsp;&nbsp;户&nbsp;&nbsp;名" id="userName1" />
                        <label style="color: red;" id="userNameError"></label>
                    </div>
                    <div>
                        <input style="margin:0" type="password" class="form-control" placeholder="密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码" id="password1" />
                        <label style="color: red;" id="pwdError"></label>
                    </div>
                    <div>
                        <input style="margin:0" type="password" class="form-control" placeholder="请确认密码" id="rePassword" />
                        <label style="color: red;" id="rePwdError"></label>
                    </div>
                    <div>
                        <input style="margin:0" type="tel" class="form-control" placeholder="手机号码" id="phone" />
                        <label style="color: red;" id="phoneError"></label>
                    </div>
                    <div style="margin-top: 0px;margin-bottom: 20px;">
                        <input type="text" class="form-control" placeholder="请输入手机验证码" id="code" style="width: 180px;margin: 0;display: inline-block;"/>
                        <button type="buttonlw" id="button1lw">获取验证码</button>
                        <label style="color: red;" id="codeError"></label>
                        <label style="color: green;" id="codeSeecuss"></label>
                    </div>

                    <div>
                        <button class="btn btn-default submit"  id="regist"	>注册</button>
                        <button class="btn btn-default submit" type="reset">重置</button>
                    </div>

                    <div class="clearfix"></div>

                    <div class="separator">
                        <p class="change_link">已有账号 ?
                            <a href="#signin" class="to_register"> 登录 </a>
                        </p>

                        <div class="clearfix"></div>
                        <br />

                        <div>
                            <h1><i class="fa fa-paw"></i>美的&nbsp;&nbsp;&nbsp;集团</h1>
                            <p>©2019 All Rights Reserved. </p>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
</div>
</body>
<script src="js/jquery.min.js"></script>
<script>
    $(function  () {

        $("#login").click(function () {
            var uPattern = /^[a-zA-Z][a-zA-Z0-9_-]{4,11}$/;
            var pPattern = /^[\w_-]{6,15}$/;
            var $userName = $("#userName").val();
            var $pwd = $("#pwd").val();
            if ($userName == 0 || $userName.length == 0) {
                $("#span2").html("用户名不能为空");
            } else if (!(uPattern.test($userName))) {
                $("#span2").html("请填写正确的用户名");
            } else {
                if ($pwd == 0 || $pwd.length == 0) {
                    $("#span2").html("密码不能为空");
                } else if (!(pPattern.test($pwd))) {
                    $("#span2").html("请填写正确的密码");
                } else {
                    $.ajax({
                        type: "post",
                        url: "http://localhost:8081/first/login",
                        dataType: "json",
                        data: {"userName": $userName, "pwd": $pwd},
                        xhrFields: {withCredentials: true},
                        success: function (data) {
//              	document.cookie="token="+data.map['token']+";expires=Session";

                            if (data.message == "yes") {
                                window.sessionStorage.setItem('token', data.map['token']);
                                location.href = ("index.html");

                            } else if (data.message == "nopwd") {
                                $("#span2").html("密码错误");
                                $("#userName").val($userName);
                                $("#pwd").val($pwd);
                            } else {
                                $("#span2").html("用户名不存在");
                                $("#userName").val($userName);
                                $("#pwd").val($pwd);
                            }
                        },
                        error: function () {
                            alert("错误");
                        }
                    });
                }
            }

        })

        // 注册
        var flag = true;
        var yanz = null;
        var uPattern = /^[a-zA-Z][a-zA-Z0-9_-]{4,11}$/;
        var pPattern = /^[\w_-]{6,15}$/;
        $("#userName1").bind("blur",_userName1);
        $("#phone").bind("blur",_phone);
        $("#code").bind("blur",_code);
        $("#password1").bind("blur",_password1);
        $("#rePassword").bind("blur",_rePassword);

        function _userName1(){
            var $userName = $("#userName1").val();
            if ($userName == ""){
                $("#userNameError").html("用户名不能为空");
                return false;
            }else if (!(uPattern.test($userName))){
                $("#userNameError").html("请填写正确的用户名");
                return false;
            }else {
                $.ajax({
                    type:"post",
                    xhrFields:{withCredentials:true},
                    url:" http://localhost:8081/first/hasUserName",
                    dataType:"json",
                    data:{"userName":$userName},
                    success:function(data) {
                        if(data.message =="yes"){
                            $("#userNameError").html("");
                        }else if(data.message =="no"){
                            $("#userNameError").html("用户名已存在");
                        }
                    },
                    error:function(){
                        alert("错误");
                    }
                });
                $("#userNameError").html("");
                return true;
            }
        }
        function _phone(){
            var $phone = $("#phone").val();
            if ($phone == "" ){
                $("#phoneError").html("手机号码不能为空");
                return false;
            }else if (!(/^1[34578]\d{9}$/.test($phone))){
                $("#phoneError").html("请输入正确的手机号码");
                return false;
            }else {
                $.ajax({
                    type:"post",
                    xhrFields:{withCredentials:true},
                    url:" http://localhost:8081/first/hasPhone",
                    dataType:"json",
                    data:{"phone":$phone},
                    success:function(data) {
                        if(data.message =="yes"){
                            $("#phoneError").html("");
                            return true;
                        }else if(data.message =="no"){
                            $("#phoneError").html("电话已存在");
                            return false;
                        }
                    },
                    error:function(){
                        alert("错误");
                    }
                });
            }
        }
        function _code(){
            var $code = $("#code").val();
            if ($code == ""){
                $("#codeError").html("验证码不能为空");
                $("#codeSeecuss").html("");
                return false;
            }else {
                $("#codeError").html("");
                return true;
            }
        }
        function _password1(){
            var $password = $("#password1").val();
            if ($password == ""){
                $("#pwdError").html("密码不能为空");
                return false;
            }else if (!(pPattern.test($password))){
                $("#pwdError").html("请填写正确的密码");
                return false;
            }else {
                $("#pwdError").html("");
                return true;
            }
        }
        function _rePassword(){
            var $password = $("#password1").val();
            var $rePassword = $("#rePassword").val();
            if ($rePassword == ""){
                $("#rePwdError").html("密码不能为空");
                return false;
            }else if($rePassword != $password){
                $("#rePwdError").html("密码请输入一致");
                return false;
            }else {
                $("#rePwdError").html("");
                return true;
            }
        }
        $("#button1lw").click(function(){
            var $phone = $("#phone").val();
            if ($phone == "" ){
                $("#phoneError").html("手机号码不能为空");
            }else if (!(/^1[34578]\d{9}$/.test($phone))){
                $("#phoneError").html("请输入正确的手机号码");
            }else {
                $.ajax({
                    type:"post",
                    xhrFields:{withCredentials:true},
                    url:" http://localhost:8081/first/hasPhone",
                    dataType:"json",
                    data:{"phone":$phone},
                    success:function(data) {
                        if(data.message =="yes"){

                            $.ajax({
                                type:"post",
                                xhrFields:{withCredentials:true},
                                url:" http://localhost:8081/first/getCode2",
                                dataType:"json",
                                data:{"phone":$phone},
                                success:function(data) {
                                    if(data.message =="yes"){
                                        $("#codeSeecuss").html("请在60s内填写");
                                    }else if(data.message =="no"){
                                        $("#codeError").html("发送失败");
                                    }
                                },
                                error:function(){
                                    alert("错误");
                                }
                            });
                            $("#phoneError").html("");
                            return true;
                        }else if(data.message =="no"){
                            $("#phoneError").html("电话已存在");
                            return false;
                        }
                    },
                    error:function(){
                        alert("错误");
                    }
                });

            }
        })
        $("#regist").click(function () {
            if(_userName1()==false){
                return false;
            }else if(_password1()==false){
                return false;
            }else if(_rePassword()==false){
                return false;
            }else if(_phone()==false){
                return false;
            }else if(_code()==false){
                 return false;
             }else{
                var $phone = $("#phone").val();
                var $password = $("#password1").val();
                var $code = $("#code").val();
                var $userName = $("#userName1").val();
                $.ajax({
                    type:"post",
                    xhrFields:{withCredentials:true},
                    url:" http://localhost:8081/first/register",
                    dataType:"json",
                    data:{"phone":$phone,"password":$password,"code":$code,"userName":$userName},
                    success:function(data) {
                        if(data.message =="yes"){
                            window.location.href=("login.html");
                        }else if(data.message =="no"){
                            $("#codeError").html("注册失败");
                        }else{
                            $("#codeError").html("验证码错误");
                            $("#codeSeecuss").html("");
                        }
                    },
                    error:function(){
                        alert("错误");
                    }
                });
                return true;
            }
        })

    })
</script>
</html>